﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml;
using System.Text;
using umbraco.cms.businesslogic.web;
using umbraco.presentation.nodeFactory;
using AD_Notifications_Dashboard.Helpers;


namespace AD_Notifications_Dashboard
{
    public partial class test2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            var doc = new XmlDocument();
            doc.Load(HttpContext.Current.Server.MapPath("data/Notifications.xml"));
            
            var nodes = doc.SelectNodes("/data/node");

            StringBuilder resultRows = new StringBuilder();

            umbraco.BusinessLogic.User u = umbraco.helper.GetCurrentUmbracoUser();
          

            foreach (XmlNode node in nodes)
            {

               

                TableRow tr = new TableRow();
                TableCell tdName = new TableCell();
                TableCell tdDate = new TableCell();
                TableCell tdEditBy = new TableCell();
                TableCell tdPublish = new TableCell();
                TableCell tdPreview = new TableCell();

                LinkButton lnkPublish = new LinkButton();
                 //Content Maintenance Dashboard Package
            
                
                tdName.Text = "<a href=\"/umbraco/editContent.aspx?id=" + node["id"].InnerText + "\" title=\"Edit\">" + node["name"].InnerText + "</a>";
                DateTime myDateTime = DateTime.Parse(node["date"].InnerText);
                tdDate.Text = myDateTime.ToString("MMM dd h:mm tt");
                tdEditBy.Text = node["user"].InnerText;
                lnkPublish.ToolTip = "Publish";
                lnkPublish.Text = "<img src=\"/umbraco/images/editor/saveAndPublish.gif\"/>";
                lnkPublish.CommandArgument = node["id"].InnerText;
                lnkPublish.Click += new System.EventHandler(this.Calculate);
                tdPublish.Controls.Add(lnkPublish);
                tdPreview.Text = "<a href=\"javascript:void(0);\" onclick=\"window.open('../" + node["id"].InnerText + ".aspx?umbVersion=" + node["version"].InnerText + "','umbPreview')\"><img src=\"/umbraco/images/editor/vis.gif\"/></a>";
                tdPreview.ToolTip = "Preview";
                
                tr.Cells.Add(tdName);
                tr.Cells.Add(tdDate);
                tr.Cells.Add(tdEditBy);
                tr.Cells.Add(tdPublish);
                tr.Cells.Add(tdPreview);
                
                Table1.Rows.Add(tr);

             
            }
            

            DivPendingApprovals.InnerHtml = resultRows.ToString();


        }

        protected void checkIfAlreadyPublished(int id) {

            Node myNode = new Node(1234);
            DateTime nodeCreationDate = myNode.UpdateDate;

        }

        protected void Calculate(object sender, System.EventArgs e)
        {
            LinkButton publishButton = sender as LinkButton;
            int publishId = Convert.ToInt32(publishButton.CommandArgument);
            umbraco.BusinessLogic.User u = umbraco.helper.GetCurrentUmbracoUser();
            UmbracoHelper.PublishItem(publishId, u.Id.ToString());
            
            
        }
    }
}
